package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.TransferInOutStockApproval;
import com.turing.post.cardmanagement.util.TransferInOutStockApprovalDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "cardmanagement.impl.TransferInOutStockApprovalDaoImpl")
@Remote
@Local
public class TransferInOutStockApprovalDaoImpl implements
		TransferInOutStockApprovalDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public TransferInOutStockApproval getTransferInOutStockApprovalByTransferApprovalId(
			BigInteger TransferApprovalId) {
		return _em.find(TransferInOutStockApproval.class, TransferApprovalId);
	}

	@Override
	public void insertTransferInOutStockApproval(TransferInOutStockApproval vo)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, vo);
	}

	@Override
	public void updateTransferInOutStockApproval(TransferInOutStockApproval vo)
			throws PCPSApplicationException {
		GeneralDao.update(_em, vo);
	}

	@Override
	public void deleteTransferInOutStockApproval(TransferInOutStockApproval vo)
			throws PCPSApplicationException {
		GeneralDao.remove(_em, vo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TransferInOutStockApproval> getTransferInOutStockApprovalList(
			PCPSWebInput webInput, String approvalBranchNo, String branchNo,
			BigInteger cardFaceId, String status, String startTime,
			String endTime, Integer page) {
		String sql = "select * from TransferInOutStockApproval where ";
		sql += "  (?1 = -1 or cardFaceId = ?1) ";
		sql += " and (?2 = -1 or status = ?2) ";
		sql += " and tranTime >= to_date(?3,'yyyy-MM-dd hh24:mi:ss') ";
		sql += " and tranTime <= to_date(?4,'yyyy-MM-dd hh24:mi:ss') ";
		if (!PCPSUtil.isEmpty(approvalBranchNo) && !PCPSUtil.isEmpty(branchNo)) {
			sql += " and (approvalBranchNo = ?5 or branchNo = ?6) ";
		} else {
			sql += " and (?5 is null or approvalBranchNo = ?5) ";
			sql += " and (?6 is null or branchNo = ?6) ";
		}
		sql += " order by tranTime desc ";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql,
				TransferInOutStockApproval.class);
		query.setFirstResult(startPosition);
		query.setMaxResults(maxResult);
		query.setParameter(1, cardFaceId);
		query.setParameter(2, status);
		query.setParameter(3, startTime + " 00:00:00");
		query.setParameter(4, endTime + " 23:59:59");
		query.setParameter(5, approvalBranchNo);
		query.setParameter(6, branchNo);
		return query.getResultList();
	}

	@Override
	public Integer getTransferInOutStockApprovalCount(PCPSWebInput webInput,
			String approvalBranchNo, String branchNo, BigInteger cardFaceId,
			String status, String startTime, String endTime) {
		String sql = "select nvl(count(*),0) from TransferInOutStockApproval where ";
		sql += " (?1 = -1 or cardFaceId = ?1) ";
		sql += " and (?2 = -1 or status = ?2) ";
		sql += " and (?3 is null or tranTime >= to_date(?3,'yyyy-MM-dd hh24:mi:ss'))";
		sql += " and (?4 is null or tranTime <= to_date(?4,'yyyy-MM-dd hh24:mi:ss')) ";
		if (!PCPSUtil.isEmpty(approvalBranchNo) && !PCPSUtil.isEmpty(branchNo)) {
			sql += " and (approvalBranchNo = ?5 or branchNo = ?6) ";
		} else {
			sql += " and (?5 is null or approvalBranchNo = ?5) ";
			sql += " and (?6 is null or branchNo = ?6) ";
		}
		sql += PCPSUtil.getSqlCond(webInput, approvalBranchNo,
				"ApprovalBranchNo");
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, cardFaceId);
		query.setParameter(2, status);
		if (PCPSUtil.isEmpty(startTime)) {
			query.setParameter(3, "");
		} else {
			query.setParameter(3, startTime + " 00:00:00");
		}
		if (PCPSUtil.isEmpty(endTime)) {
			query.setParameter(4, "");
		} else {
			query.setParameter(4, endTime + " 23:59:59");
		}
		query.setParameter(5, approvalBranchNo);
		query.setParameter(6, branchNo);
		return Integer.parseInt(query.getSingleResult().toString());
	}

}
